﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using InfoJetSoft.Service.Entity;
using Microsoft.SharePoint;

//CID 0930 //2011.05.27 //Begin
namespace InfoJetSoft.Service.Util
{
    class ExportFileUtil
    {
        public static bool isExportFileEnabled(Form form)
        {
            if (ConfigUtil.InlineFilePath() == null && form.JetContext.ExportFileStorage == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //folderName只由InlineFilePath使用.
        public static string GetExportFilePath(Form form, string fileId, string fileName)
        {
            if (form.JetContext.ExportFileStorage != null)
            {
                string path = form.JetContext.ExportFileStorage.GetExportFilePath(fileId, fileName);
                return path;
            }
            else
            {
                //CID 0004 //2008.05.31 //Begin
                string inlineFilePath = InlineFileUtil.getFormInlineFileDiskPath(form); //CID 0033 //2008.06.25 //Method.
                //CID 0004 //2008.05.31 //End
                if (inlineFilePath != null)
                {
                    //CID 0004 //2008.06.01 //Begin
                    //这里要求inlineFilePath必须是一个绝对路径.
                    //这里需要inlineFilePath是一个Web路径,因为要输出到javascript中执行window.open()(FormExportMhtCommandWorker),"\"将被javascript理解为转义符.
                    string path = inlineFilePath.Replace(System.IO.Path.DirectorySeparatorChar.ToString(), "/") + "/" + fileId + "/" + fileName;
                    return path;
                    //CID 0004 //2008.06.01 //End
                }
            }
            return null;
        }

        public static void ExportFileContent(Form form, string fileId, string fileName, byte[] content)
        {
            if (form.JetContext.ExportFileStorage != null)
            {
                form.JetContext.ExportFileStorage.ExportFileContent(fileId, fileName, content);
            }
            else
            {
			//Modified by lishuchao 2013.7.15
                //string inlineFilePath = InlineFileUtil.getFormInlineFileDiskPath(form);
                //string folderPath = inlineFilePath + System.IO.Path.DirectorySeparatorChar + fileId;
                //string cachePath = form.HttpContext.Request.MapPath(folderPath);
                //Directory.CreateDirectory(cachePath);
                //cachePath = cachePath + Path.DirectorySeparatorChar + fileName;

                //FileStream output = File.Create(cachePath);
                //output.Write(content, 0, content.Length);
                //output.Close();
                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;
                SPFolder folder = web.Lists["InfoBus_Att"].RootFolder.SubFolders.Add(fileId);

                folder.Files.Add(fileName, content,true);
                web.AllowUnsafeUpdates = false;
            }
        }
    }
}
//CID 0930 //2011.05.27 //End
